package com.wanxi.tcp.demo5客户端发送图片给服务器;

import java.io.*;
import java.net.Socket;

/**
 * TCP 客户端,演示客户端上传图片文件到服务器端
 */
public class TCPClient {
    public static void main(String[] args) throws IOException {
        System.out.println("tcp 客户端启动......");
        //1 创建客户端socket
        Socket clientSocket = new Socket("127.0.0.1", 40000);
        //2 创建要读取的文件流
        InputStream inputStream = new FileInputStream(new File("day22网络编程基础/src/com/wanxi/tcp/demo5客户端发送图片给服务器/星语餐饮-02.jpg"));
        //3 创建输出流
        OutputStream outputStream = clientSocket.getOutputStream();
        int content = -1;
        // 输出数据
        System.out.println("客户端上传数据....");
        while ((content = inputStream.read()) != -1) {
            outputStream.write(content);
        }
        //shutdownOutput 给服务器一个结束标记,告诉服务器输出操作已经传输完毕,但socket不关闭。
        clientSocket.shutdownOutput();

        BufferedReader br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        String line;
        while ((line = br.readLine()) != null) {
            System.out.println("服务器说：" + line);
        }

        // 释放连接，释放资源
        outputStream.close();
        clientSocket.close();
    }
}
